﻿var ActiveIndex=0;

function ClientAddVote() {
    belongSiteIndex = document.getElementById("drpSelectSiteForVote").selectedIndex;
    belongSite = document.getElementById("drpSelectSiteForVote").options[belongSiteIndex].value;
    Url = Portal.CMS.Vote.GetAddNewUrl(belongSite).value;
    window.open(Url, "_blank");
//    window.showModalDialog(Url, "", "dialogHeight: 600px; dialogWidth: 700px; dialogTop: 10px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; scroll: Yes; status: Yes;"); 
//    value=Portal.CMS.Vote.GetHtml().value;
//    if(value.indexOf("ERROR")==-1)
//    {
//        document.getElementById("divVoteMain").innerHTML=value;
//    }
}

function ClientSaveAddVote() {
    belongSite = document.getElementById("txtBelongSite").value;
    language=document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;
    Subject=document.getElementById("txtSubject").value;
    VoteItems=Portal.CMS.AddVote.GetVoteItemsObject().value;
    for(var Index=0;Index<VoteItems.length;Index++)
    {
        VoteItems[Index].VoteItemName=document.getElementById("txtItemName_"+Index.toString()).value;
        VoteItems[Index].IndexOrder=Index+1;
    }
    retValue = Portal.CMS.AddVote.ServerSideAddVote(belongSite,language, null, Subject, VoteItems).value;
    if(retValue.indexOf("ERROR")!=-1)
    {
        alert(retValue);
        return;
    }
    alert(retValue);
    window.close();
}


function ClientDeleteVote(VoteId)
{
    Msg=Portal.CMS.Vote.GetConfirmDeleteMsg().value;
    if(confirm(Msg)==false)return;
    RetValue = Portal.CMS.Vote.ServerSideDeleteVote(VoteId).value;
    if(RetValue.indexOf("ERROR")!=-1)
    {
        alert(RetValue);
        return;
    }
    alert(RetValue);
    ClientReadingVote();
}

function ShowClientUpdateUrlForm(VoteId)
{
    Url=Portal.CMS.Vote.GetShowUpdateForm(VoteId).value;
    if(Url.indexOf("ERROR")!=-1)
    {
        alert(Url);
        return;
    }
    window.open(Url, "_blank");
//    window.showModalDialog(Url, null, 'dialogHeight: 600px; dialogWidth: 700px; dialogTop: 10px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; scroll: Yes; status: Yes;'); 
//    value=Portal.CMS.Vote.GetHtml().value;
//    if(value.indexOf("ERROR")==-1)
//    {
//        document.getElementById("divVoteMain").innerHTML=value;
//    }
}

function ShowFormAppendLink(VoteId)
{
    Url=Portal.CMS.UpdateVote.GetShowAppendForm(VoteId).value;
    if(Url.indexOf("ERROR")!=-1)
    {
        alert(Url);
        return;
    }
    window.showModalDialog(Url, null, 'dialogHeight: 600px; dialogWidth: 700px; dialogTop: 10px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; scroll: Yes; status: Yes;'); 
    language=document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;
    
    value=Portal.CMS.UpdateVote.GetHtml(language,VoteId).value;
    if(value.indexOf("ERROR")==-1)
    {
        document.getElementById("divFormContent").innerHTML=value;
    }
}

function ClientSaveUpdateVote(VoteId)
{
    language=document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;
    Subject=document.getElementById("txtVote").value;
    VoteItems=Portal.CMS.UpdateVote.GetVoteItemsObject().value;
    for(var Index=0;Index<VoteItems.length;Index++)
    {
        if(document.getElementById("txtItemName_"+Index.toString())!=null)
        {
            if(document.getElementById("txtVoteItemId_"+Index.toString())!=null)
            {
                VoteItems[Index].VoteItemId=document.getElementById("txtVoteItemId_"+Index.toString()).value;
            }
            VoteItems[Index].VoteItemName=document.getElementById("txtItemName_"+Index.toString()).value;
            VoteItems[Index].IndexOrder=document.getElementById("txtIndexOrder_"+Index.toString()).value;
        }
    }
    RetValue = Portal.CMS.UpdateVote.ServerSideUpdateVote(language,VoteId,Subject,VoteItems).value;
    if(RetValue.indexOf("ERROR")!=-1)
    {
        alert(RetValue);
        return;
    }
    alert(RetValue);
    //window.close();
}

function AppendNewRow()
{
    document.getElementById("divContentItems").innerHTML= document.getElementById("divContentItems").innerHTML+Portal.CMS.AddVote.ServerSideNewLinkItem(ActiveIndex).value;
    ActiveIndex++;
    alert(ActiveIndex);
}

function RemoveItem(Index)
{
    document.getElementById("divRowItem_"+Index).innerHTML="";
    Html=document.getElementById("divContentItems").innerHTML;
    document.getElementById("divContentItems").innerHTML=Portal.CMS.AddVote.ProcessHtml(Html,Index).value;
}


function DetectLanguageBody(VoteId)
{
    language=document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;
    value=Portal.CMS.UpdateVote.GetHtml(language,VoteId).value;
    if(value.indexOf("ERROR")!=-1)
    {
        alert(value);
        return;
    }
    else
    {
        document.getElementById("divFormContent").innerHTML=value;
    }
    
    value=Portal.CMS.UpdateVote.GetQuestion(language,VoteId).value;
     if(value.indexOf("ERROR")!=-1)
    {
        alert(value);
        return;
    }
    else
    {
        document.getElementById("txtVote").value=value;
    }
}

function ClientReadingVote() {
    ProcessingText = Portal.CMS.Vote.GetProcessingText().value;
    document.getElementById("divProcessing").innerHTML = ProcessingText;
    setTimeout("RealClientReadingVote()", 10);
}

function RealClientReadingVote() {
    belongSiteIndex = document.getElementById("drpSelectSiteForVote").selectedIndex;
    belongSite = document.getElementById("drpSelectSiteForVote").options[belongSiteIndex].value;

    Html = Portal.CMS.Vote.GetHtml(belongSite).value;
    document.getElementById("divProcessing").innerHTML = "";
    document.getElementById("divVoteMain").innerHTML = Html;
}


function ClientSyncWithServer(VoteId) {
    if (confirm(Portal.CMS.Vote.GetConfirmSyncAllMsg().value) == false) {
        return;
    }
    document.getElementById("divProcessing").innerHTML = Portal.CMS.Vote.GetProcessingText().value;
    setTimeout("RealClientSyncWithServer('" + VoteId + "')", 10);
}

function RealClientSyncWithServer(VoteId) {
    rValue = Portal.CMS.Vote.ServerSideSyncWithServer(VoteId).value;
    document.getElementById("divProcessing").innerHTML = "";
    alert(rValue);
}